package com.wanhelibs;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;


import com.google.gson.Gson;
import com.wanhe.base.model.base.Dish;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.base.util.StringFileUtil;
import com.wanhe.mealbookmodule.AppConf;
import com.wanhe.mealbookmodule.GeneratorInfo;
import com.wanhe.viewconf.ViewCreator;
import com.wanhe.viewconf.ViewOptions;
import com.wanhe.viewconf.generator.IViewGenerator;
import com.wanhe.viewconf.views.child.BaseXYView;
import com.wanhe.viewconf.views.child.MealBookChildItemView;
import com.wanhe.viewconf.views.child.MealBookImageView;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;

/**
 * Instrumentation test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

	public static final String TAG = "ExampleInstrumentedTest";

	@Test
	public void useAppContext() throws Exception {
		// Context of the app under test.
		Context appContext = InstrumentationRegistry.getTargetContext();

		assertEquals("com.wanhelibs", appContext.getPackageName());
	}

	/**
	 * Methor Name:testViewFactoryFunc<br/>
	 * Package Name:com.wanhelibs<br/>
	 * Date:2017/7/4 10:02<br/>
	 * author:chendeji<br/>
	 * desc: 用于测试控件加载器加载配置文件功能的测试api<br/>
	 * Copyright (c) 2017/7/4, 781571323@qq.com All Rights Reserved.
	 *
	 * @param
	 * @return
	 *
	 */
	@Test
	public void testViewFactoryFunc() {
		Context appContext = InstrumentationRegistry.getTargetContext();
		/**
		 *
		 * 加载控件配置的配置文件
		 *
		 * @author 陈德基
		 * @time 2017/7/4 9:37
		 */
		InputStream open = null;
		try {
			open = appContext.getAssets().open("appConfig.json");
			String viewConfigJson = StringFileUtil.convertStreamToString(open);
			final AppConf appConf = new Gson().fromJson(viewConfigJson, AppConf.class);
			assertTrue(appConf.viewConfigs.size() > 0);

			ViewCreator.getInstance().init(ViewCreator.ViewConfig.builder()
					.txtFontPath(appConf.com_tf)
					.extentionPT(1.0f)
					.salloutImg(appContext.getResources().getDrawable(R.drawable.sall_out_light))
					.couterImg(appContext.getResources().getDrawable(R.drawable.plus_nor_bg),
							appContext.getResources().getDrawable(R.drawable.reduce_nor_bg))
					.counterIconTypeface(appConf.counter_tf)
					.counterWithIMG(appConf.isPlusText)
					.init(new ViewCreator.ViewConfig.Builder.OnInitMapData() {
						@Override
						public void initMap(Map<String, String> map) {
							List<GeneratorInfo> viewConfigs = appConf.viewConfigs;
							if (ObjectUtil.checkObjectList(viewConfigs)) {
								for (int i = 0; i < viewConfigs.size(); i++) {
									String key = viewConfigs.get(i).key;
									String gen = viewConfigs.get(i).gen;
									map.put(key, gen);
								}
							}
						}

						@Override
						public void extentionGenerator(String genName, Map<String, IViewGenerator> generatorMap) {

						}

						@Override
						public void onErr(Throwable throwable) {

						}
					})
					.build());

			ViewCreator.getInstance().setOptions(ViewOptions.builder()
					.setCounterViewClickListener(new ViewOptions.OnCounterViewClickListener() {
						@Override
						public void plus(MealBookChildItemView view, Dish dish) {

						}

						@Override
						public void reduce(MealBookChildItemView view, Dish dish) {

						}
					})
					.setViewNormalClickListener(new ViewOptions.OnViewNormalClickListener() {
						@Override
						public void cartChangeReceiveXY(BaseXYView view, int x, int y) {

						}

						@Override
						public void operationView(BaseXYView view, int opType) {

						}
					})
					.build());

			IViewGenerator gif = ViewCreator.getInstance().createXYView("gif");
			BaseXYView view = gif.createView(appContext);
			assertTrue(view != null);
			assertTrue(view instanceof MealBookImageView);

			assertFalse(ViewCreator.getInstance().isPlusIconUseText());
			assertEquals(1.0f, ViewCreator.getInstance().getExtentionPt(), 0.0f);
			assertEquals("font/com_tf", ViewCreator.getInstance().getFontTypeface());
			assertEquals("font/counter_tf", ViewCreator.getInstance().getIconTypeface());
			assertTrue(ViewCreator.getInstance().getCounterPlusDrawble() != null);
			assertTrue(ViewCreator.getInstance().getCounterReduceDrawable() != null);

			assertTrue(ViewCreator.getInstance().getViewOptions().getCounterViewClick() != null);
			assertTrue(ViewCreator.getInstance().getViewOptions().getViewNormalClick() != null);


		} catch (IOException e) {
			e.printStackTrace();
			assertTrue(false);
		}
	}

}
